import React from 'react'
import {observable, action} from 'mobx'
import {observer} from 'mobx-react'

let shop=observable({
        products:[
            {id:"111",name:"电脑",price:"10000"},
            {id:"222",name:"键盘",price:"1000"}
        ],
        shoppingcar:{
            items:[
                {id:"111",name:"电脑",price:"10000",count:1}
            ]
        }
})

shop.pinkProduct=action((item)=>{
   let exist=false
   let newitem=shop.shoppingcar.items.map((p)=>{
       if(p.id===item.id){
           exist=true
           return {...p,count:p.count+1}
       }
       else{
           return p
       }
   })
   if(!exist){
    shop.shoppingcar.items.push({...item,count:1})
   }
   else{
    shop.shoppingcar.items=newitem
   }
})

shop.change=action((item ,n)=>{
    if(item.count+n>0){
        item.count+=n
    }
})

sh
export default shop

export function connect(WrapComponent){
    const  Component=observer(WrapComponent)
    return function(props){
        return (
            <Component shop={shop} {...props}/>
        )
    }
}